var gulp = require("gulp"),
  uglify = require("gulp-uglify"),
  cssmin = require("gulp-minify-css"),
  htmlmin = require("gulp-htmlmin"),
  htmlclean = require("gulp-htmlclean"),
  concat = require("gulp-concat");

//JS压缩
gulp.task("uglify", function () {
  return gulp
    .src(["./public/js/**/.js", "!./public/js/**/*min.js"]) //只是排除min.js文件还是不严谨，一般不会有问题，根据自己博客的修改我的修改为return gulp.src(['./public/**/*.js','!./public/zuoxi/**/*.js',,'!./public/radio/**/*.js'])
    .pipe(uglify())
    .pipe(gulp.dest("./public/js")); //对应修改为./public即可
});
//public-fancybox-js压缩
gulp.task("fancybox:js", function () {
  return gulp
    .src("./public/js/*.js")
    .pipe(uglify())
    .pipe(gulp.dest("./public/js/"));
});
// 合并 JS
gulp.task("jsall", function () {
  return (
    gulp
      .src("./public/**/*.js")
      // 压缩后重命名
      .pipe(concat("app.js"))
      .pipe(gulp.dest("./public"))
  );
});
//public-fancybox-css压缩
gulp.task("fancybox:css", function () {
  return gulp
    .src("./public/css/*.css")
    .pipe(cssmin())
    .pipe(gulp.dest("./public/css/"));
});
//CSS压缩
gulp.task("cssmin", function () {
  return gulp
    .src(["./public/css/*.css", "!./public/css/*min.css"])
    .pipe(cssmin())
    .pipe(gulp.dest("./public/css/"));
});

// 压缩 public 目录 html文件 public/**/*.hmtl 表示public下所有文件夹中html，包括当前目录
gulp.task("minify-html", function () {
  return gulp
    .src("./public/**/*.html")
    .pipe(htmlclean())
    .pipe(
      htmlmin({
        collapseInlineTagWhitespace: true,
        collapseWhitespace: true,
        // 通过minifier去掉条件注释内容
        processConditionalComments: true,
        // 去掉注释
        removeComments: true,
        minifyJS: {
          keep_fargs: true, // 防止丢弃函数参数或修改参数
          mangle: {
            toplevel: true,
          },
          nameCache: {},
        },
        minifyCSS: true,
        minifyURLs: true,
      })
    )
    .pipe(gulp.dest("./public"));
});
// copy文件paste到public里去
/* gulp.task("copy-web_config", function () {
  return gulp.src("./web.config").pipe(gulp.dest("./public"));
}); */
/*
gulp.series：按照顺序执行
gulp.parallel：可以并行计算
*/
gulp.task(
  "build",
  gulp.series(
    gulp.parallel([
      "uglify",
      "cssmin",
      "fancybox:js",
      "fancybox:css",
      // "jsall",
      "minify-html",
    ]),
    (done) => {
      done();
    }
  )
);
